/*
 * Copyright (C) 2011 Patrik Akerfeldt
 * Copyright (C) 2011 Jake Wharton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.handpay.zztong.hp.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * Draws circles (one for each view). The current view position is filled and
 * others are only stroked.
 */
public class newCirclePageIndicator extends View 
{
	private int mCurrentPage = -1;
	private int mTotalPage = 0;
	/**
	 * 指示器默认背景色
	 */
	private int defaultBackgroundColor=Color.rgb(67, 67, 67);
	/**
	 * 指示器选择背景色
	 */
	private int selectBackgroundColor=Color.rgb(247, 248, 248);
	/**
	 * 小圆点默认大小
	 */
	private int defaultCircleSize=10;
	/**
	 * 选择时的大小
	 */
	private int selectCicleSize=5;
	private int changeTimeInterval=5000;
	
	public newCirclePageIndicator(Context context) {
		super(context);
	}

	public newCirclePageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public newCirclePageIndicator(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
    
	public void setTotalPage(int nPageNum) {
        mTotalPage = nPageNum;
        if (mCurrentPage >= mTotalPage)
            mCurrentPage = mTotalPage - 1;
    }
	
	public int getCurrentPage() {
        return mCurrentPage;
    }
	
	public void setCurrentPage(int nPageIndex) {
        if (nPageIndex < 0 || nPageIndex >= mTotalPage)
            return;
        if (mCurrentPage != nPageIndex) {
            mCurrentPage = nPageIndex;
            this.invalidate();
        }
    }
	
	public void setDefaultBackgroundColor(int defaultBackgroundColor) {
		this.defaultBackgroundColor = defaultBackgroundColor;
	}

	public void setSelectBackgroundColor(int selectBackgroundColor) {
		this.selectBackgroundColor = selectBackgroundColor;
	}
	
	public void setDefaultCircleSize(int defaultCircleSize) {
		this.defaultCircleSize = defaultCircleSize;
	}

	public void setSelectCicleSize(int selectCicleSize) {
		this.selectCicleSize = selectCicleSize;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(defaultBackgroundColor);
        Rect r = new Rect();
        this.getDrawingRect(r);
        int iconWidth = 20;
        int iconHeight = 20;
        int space = 12;
        int x = (r.width() - (iconWidth * mTotalPage + space * (mTotalPage - 1))) / 2;
        int y = (r.height() - iconHeight) / 2;
//        for (int i = 0; i < mTotalPage; i++) {
//            int resid = R.drawable.point_unselect;
//            if (i == mCurrentPage) {
//                resid = R.drawable.point_select;
//            }
//            Rect r1 = new Rect();
//            r1.left = x;
//            r1.top = y;
//            r1.right = x + iconWidth;
//            r1.bottom = y + iconHeight;
//            Bitmap bmp = BitmapFactory.decodeResource(getResources(), resid);
//            canvas.drawBitmap(bmp, null, r1, paint);
//            x += iconWidth + space;
//        }
        for (int i = 0; i < mTotalPage; i++) {
            if (i == mCurrentPage) {
            	paint.setColor(selectBackgroundColor);
            }else{
            	paint.setColor(defaultBackgroundColor);
            }
            if(i == mCurrentPage){
            	paint.setColor(defaultBackgroundColor);
            	canvas.drawCircle(x,   y, defaultCircleSize, paint);
            	paint.setColor(selectBackgroundColor);
            	canvas.drawCircle(x,   y, selectCicleSize, paint);
            }else{
            	canvas.drawCircle(x,   y, defaultCircleSize, paint);
            }
            x += iconWidth + space;
        }
	}
}
